package com.xj.exportfile.service;

import cn.hutool.core.util.ZipUtil;
import com.xj.exportfile.entity.User;
import com.xj.exportfile.util.ExcelUtils;
import com.xj.exportfile.util.PdfUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DemoService {
    /**
     * 获取用户列表
     * @return
     */
    public List<User> selectUserList() {
        List<User> list = new ArrayList<>();
        list.add(new User(1001, "张三", "男", "广州", "1981122222", "程序员", "个人简介", "https://img-blog.csdnimg.cn/img_convert/473fc22c37609e3d06aba4b34344f572.png", LocalDateTime.now()));
        list.add(new User(1002, "李四", "男", "深圳", "1681122222", "程序员", "个人简介", "https://img-blog.csdnimg.cn/img_convert/473fc22c37609e3d06aba4b34344f572.png", LocalDateTime.now()));
        list.add(new User(1003, "王五", "男", "上海", "1781122222", "程序员", "个人简介", "https://img-blog.csdnimg.cn/img_convert/473fc22c37609e3d06aba4b34344f572.png", LocalDateTime.now()));
        list.add(new User(1004, "赵六", "男", "北京", "1581122222", "程序员", "个人简介", "https://img-blog.csdnimg.cn/img_convert/473fc22c37609e3d06aba4b34344f572.png", LocalDateTime.now()));
        return list;
    }

    /**
     * 导出 Excel
     */
    public void exportExcel(HttpServletResponse response) {
        List<User> list = selectUserList();
        ExcelUtils<User> utils = new ExcelUtils<>(User.class);
        utils.export(response, list);
    }

    /**
     * 导出 pdf
     */
    public void exportPdf(HttpServletResponse response) {
        User user = new User(1001, "张三", "男", "广州", "1981122222", "程序员", "个人简介", "https://img-blog.csdnimg.cn/img_convert/473fc22c37609e3d06aba4b34344f572.png", LocalDateTime.now());
        try {
            ByteArrayOutputStream outputStream = PdfUtils.export(user);
            outputStream.writeTo(new FileOutputStream("D:/tmp.pdf"));
            // 返回到前端
//            OutputStream out = response.getOutputStream();
//            response.setCharacterEncoding("UTF-8");
//            response.setContentType("application/pdf");
//            out.write(outputStream.toByteArray());
//            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出成 zip
     * @param response
     * @throws Exception
     */
    public void exportZip(HttpServletResponse response) throws Exception {
        List<User> list = selectUserList();
        int size = list.size();
        InputStream[] ins = new InputStream[size];
        String[] paths = new String[size];
        // 导出到 pdf
        for (int i = 0; i < size; i++) {
            ByteArrayOutputStream outputStream = PdfUtils.export(list.get(i));
            ins[i] = new ByteArrayInputStream(outputStream.toByteArray());
            paths[i] = String.format("用户信息（编号：%s）.pdf", list.get(i).getId().toString());
            outputStream.close();
        }
        // 测试写到本地
        FileOutputStream out = new FileOutputStream("D:/tmp.zip");
        ZipUtil.zip(out, paths, ins);
        // 压缩并返回到前端
//        OutputStream out = response.getOutputStream();
//        ZipUtil.zip(out, paths, ins);
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/zip");
    }
}
